import dto.MessageFromTg
import io.ktor.client.*
import io.ktor.client.request.*
import io.ktor.http.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import kotlinx.serialization.json.Json
import org.slf4j.Logger

class AssistantImpl(
    private val ktorClient: HttpClient,
    private val logger: Logger
) : Assistant {
    companion object {
        private const val BOT_MESSAGE_URL = "/assistant/get_message"
    }

    override suspend fun getMessage(msg: MessageFromTg): Boolean =
        withContext(Dispatchers.IO) {
            val json = Json {
                prettyPrint = true
                isLenient = true
                ignoreUnknownKeys = true
                encodeDefaults = true
            }
            val req = json.encodeToJsonElement(MessageFromTg.serializer(), msg)
            println(req)

                ktorClient.post {
                    url(BOT_MESSAGE_URL)
                    contentType(ContentType.Application.Json)
                    setBody(msg)
            }
            return@withContext true
        }
}
